package POJO;

import DB.*;
import java.security.NoSuchAlgorithmException;

/**
 *
 * @author Sibrand
 */
public class User {

    public User() {
    }
    public String username;
    public String email;
    public String fullName;
    public String[] friends;
    public String[] family;
    public int[] trips;

    public boolean Register(String username, String password, String fullName, String email,
            boolean showInfo) throws NoSuchAlgorithmException {
        UserManager um = new UserManager(DBInfo.getConnection());

        if (um.addUser(username, fullName, email, password)) {
            um.changeShowInfo(username, showInfo);
            this.username = username;
            GetInfo();
            return true;
        } else {
            return false;
        }
    }

    public boolean Login(String user, String password) throws NoSuchAlgorithmException {
        UserManager um = new UserManager(DBInfo.getConnection());
        System.out.println(user);

        if (user != null && password != null && um.usernameExists(user) && um.getPassword(user) != null) {
            String hashed = um.hashPassword(password);
            if (hashed.compareTo(um.getPassword(user)) == 0) {
                username = user;
                if (GetInfo()) {
                    return true;
                } else {
                    username = null;
                    return false;
                }
            } else {
                username = null;
                return false;
            }

        } else {
            username = null;
            return false;
        }
    }

    public boolean IsLoggedIn() {
        return username != null;
    }

    public boolean GetInfo() {
        if (username != null) {
            UserManager um = new UserManager(DBInfo.getConnection());
            fullName =
                    um.getFullName(username);
            email =
                    um.getEmail(username);

            RelationManager rm = new RelationManager(um.conn);
            friends =
                    rm.getFriends(username);
            family =
                    rm.getFamily(username);

            TripManager tm = new TripManager(rm.conn);
            trips =
                    tm.getTripsOwnedBy(username);
            return true;
        } else {
            return false;
        }
    }
}